<?php
/**
 * Created by PhpStorm .
 * User: 杜磊磊
 * Date: 2020/6/24
 * Time: 15:51
 */

namespace app\admin\business;
use app\admin\model\AdminUser as AdminuserModel;

class AdminUser
{
    public $adminUserObj = null;

    public function __construct()
    {
        $this->adminUserObj = new AdminuserModel();
    }

    public function login($data)
    {
        $adminUser = $this->adminUserObj->getAdminUserByUsername($data['username']);
        if (!$adminUser){
            throw new \think\Exception('不存在该用户');
        }
        //判断密码是否正确
        if ($adminUser['password'] != md5($data['password']."_duleilei_abc")){
            //return show(config("status.error"),"密码错误");
            throw new\think\Exception("密码错误");
        }
        //需要记录信息到数据表中
        $updateData = [
            'last_login_time' => time(),
            'last_login_ip' => request()->ip(),
            'update_time' => time(),
        ];
        $res = $this->adminUserObj->updateById($adminUser ['id'],$updateData);
        if (empty($res)){
            throw new\think\Exception("登录失败");
            //return show(config("status.error"),'登陆失败');
        }
        //记录session
        session("adminUser",$adminUser);
            //return show(config("status.success"),'登陆成功');
            return true;
    }
    public function getAdminUserByUsername($username)
    {
        $adminUser = $this->adminUserObj->getAdminUserByUsername($username);
        if (empty($adminUser) || $adminUser->status != config("status.table_normal")){
            return [];
        }
        $adminUser = $adminUser->toArray();
        return $adminUser;
    }
}